//
//  AllMakeViewController.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/5.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "AllMakeViewController.h"
#import "XMHNetWorkongMethods.h"
#import "ShipingListTableViewCell.h"
#define POSTURL @"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446688240625&vc=77&vn=5.4.0&loguid=0&deviceid=haodou99000505722929&uuid=a6afb132369a979ba3427eec69189063&channel=xiaomi_v540&method=Search.getList&virtual=&signmethod=md5&v=2&timestamp=1446724423&nonce=0.6598252029325123&appsign=8df0ee106183ce750f33bc13385ef4e7"
#import "ShipingListTableViewCell.h"
#import "FitHeader.h"
#import "SingleTon.h"
#import <MJRefresh.h>
#import "ShipingPlayViewController.h"
#import "MBProgressHUD.h"
static NSInteger offset = 0;
@interface AllMakeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)NSMutableArray *listArray;
@property(nonatomic , retain)MBProgressHUD *prigressView;
@end

@implementation AllMakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
    
    //导航条返回的button
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"房子.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction)];
    
    // Do any additional setup after loading the view.
    _listArray = [NSMutableArray array];
    self.title = _name;
    [self getData];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView setTableFooterView:v];
    [self.view addSubview:_tableView];
    
    
    //加载菊花
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"请稍等...";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
    [self.view addSubview:_progressView];
    [self getData];

    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_listArray removeAllObjects];
        offset = 0;
        [self getData];
        [_tableView reloadData];
    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        offset += 20;
        [self getData];
        [_tableView reloadData];
    }];
    [self getData];
}


//button的点击方法
-(void)buttonAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark  数据解析
-(void)getData
{
    NSString *s = [NSString stringWithFormat:@"%ld",offset];
    [XMHNetWorkongMethods getDataVyString:POSTURL BodyString:[NSDictionary dictionaryWithObjectsAndKeys:@"null",@"tagid",@"606cac6b8c4a160285003ca1999d099e",@"request_id",@"20",@"limit",s,@"offset",_name,@"keyword",@"a6afb132369a979ba3427eec69189063",@"uuid", nil] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            [_listArray addObject:dic];
        }
        if (!array.count) {
            [_tableView.footer endRefreshingWithNoMoreData];
        }
        else
        {
            [_tableView.footer endRefreshing];
            [_tableView.header endRefreshing];
            [_progressView removeFromSuperview];
            [_tableView reloadData];

        }

    } WithFailure:^(NSError *error) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"网络故障" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            [alertController removeFromParentViewController];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
            [_progressView removeFromSuperview];
        }];
        UIAlertAction *going = [UIAlertAction actionWithTitle:@"继续加载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self getData];
            [alertController removeFromParentViewController];
        }];
        [alertController addAction:going];
        [self presentViewController:alertController animated:YES completion:nil];
        [alertController addAction:ok];
    }];

}


#pragma mark 设置cell部分
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FitHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    ShipingListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ShipingListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ShipingModel *model = [[ShipingModel alloc]init];
    [model setValuesForKeysWithDictionary:_listArray[indexPath.row]];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[SingleTon shareSingleTon]setRid:[[_listArray[indexPath.row]objectForKey:@"RecipeId"]stringValue]];
    ShipingPlayViewController *vc = [[ShipingPlayViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
